package com.partvoyage.server.utils;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.persistence.EntityManager;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Transaction;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import com.partvoyage.server.dao.DataInfoPointEntity;
import com.partvoyage.shared.InfoPointBean;

public class DBTool {
	// DatastoreService datastore =
	// DatastoreServiceFactory.getDatastoreService()
	// Transaction txn = datastore.beginTransaction();
	// try {
	// Key employeeKey = KeyFactory.createKey("Employee", "Joe");
	// Entity employee = datastore.get(employeeKey);
	// employee.setProperty("vacationDays", 10);
	//
	// datastore.put(employee);
	//
	// txn.commit();
	// } finally {
	// if (txn.isActive()) {
	// txn.rollback();
	// }
	// }
//	static {
//		ObjectifyService.register(DataInfoPointEntity.class);
//	}

	private static final PersistenceManagerFactory pmfInstance = JDOHelper
			.getPersistenceManagerFactory("transactions-optional");

	public static PersistenceManagerFactory getPersistenceManagerFactory() {
		return pmfInstance;
	}

	public DataInfoPointEntity createInfoPoint(Double latitude,
			Double longitude, String comment, String cout, String avecQui) {

		// Objectify ofy = ObjectifyService.begin();
		// DataInfoPointEntity tp = new DataInfoPointEntity();
		// tp.setLatitude(latitude);
		// tp.setLongitude(longitude);
		// tp.setComment(comment);
		// tp.setCout(cout);
		// tp.setAvecQui(avecQui);
		// // ofy.put(tp);
		// EntityManager em = EMF.getCreatedFactory().createEntityManager();
		// em.persist(tp);
		// em.close();
		PersistenceManager pm = getPersistenceManagerFactory()
				.getPersistenceManager();
		DataInfoPointEntity tp = new DataInfoPointEntity();
		try {

			tp.setLatitude(latitude);
			tp.setLongitude(longitude);
			tp.setComment(comment);
			tp.setCout(cout);
			tp.setAvecQui(avecQui);
			pm.makePersistent(tp);
		} finally {
			pm.close();
		}
		return tp;
	}

	public DataInfoPointEntity updateInfoPoint(Long id, Double latitude,
			Double longitude, String comment, String cout, String avecQui) {

		// Objectify ofy = ObjectifyService.begin();
		// DataInfoPointEntity tp = new DataInfoPointEntity();
		// tp.setId(id);
		// tp.setLatitude(latitude);
		// tp.setLongitude(longitude);
		// tp.setComment(comment);
		// tp.setCout(cout);
		// tp.setAvecQui(avecQui);
		// EntityManager em = EMF.getCreatedFactory().createEntityManager();
		PersistenceManager pm = getPersistenceManagerFactory()
				.getPersistenceManager();
		// String name = contact.getName();
		// String phone = contact.getPhone();
		// String email = contact.getEmail();
		DataInfoPointEntity contact;
		try {
			pm.currentTransaction().begin();
			// We don't have a reference to the selected Product.
			// So we have to look it up first,
			contact = pm.getObjectById(DataInfoPointEntity.class, id);
			contact.setLatitude(latitude);
			contact.setLongitude(longitude);
			contact.setComment(comment);
			contact.setCout(cout);
			pm.makePersistent(contact);
			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}

		// ofy.put(tp);
		return contact;
	}

	public DataInfoPointEntity findByInfoPointId(Long markerId) {
		// Objectify ofy = ObjectifyService.begin();
		// return ofy.find(DataInfoPointEntity.class, markerId);
		PersistenceManager pm = getPersistenceManagerFactory()
				.getPersistenceManager();
		return pm.getObjectById(DataInfoPointEntity.class, markerId);

	}

	public List<DataInfoPointEntity> findAllInfoPoint() {
		List<DataInfoPointEntity> infoBulleMarkers = new ArrayList<DataInfoPointEntity>();
		Objectify ofy = ObjectifyService.begin();
		Query<DataInfoPointEntity> q = ofy.query(DataInfoPointEntity.class);
		for (DataInfoPointEntity d : q) {
			infoBulleMarkers.add(d);
		}
		return infoBulleMarkers;
	}

	public void deleteAll() {
		PersistenceManager pm = getPersistenceManagerFactory()
				.getPersistenceManager();
		try {
			pm.currentTransaction().begin();

			// We don't have a reference to the selected Product.
			// So we have to look it up first,
			String query = "select from " + DataInfoPointEntity.class.getName();
			List<DataInfoPointEntity> list= (List<DataInfoPointEntity>) pm.newQuery(query).execute();
			pm.deletePersistent(list);
			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
		// Objectify ofy = ObjectifyService.begin();
		// Query<DataInfoPointEntity> q = ofy.query(DataInfoPointEntity.class);
		// ofy.delete(q);
		// DatastoreService datastore =
		// DatastoreServiceFactory.getDatastoreService()
		// Transaction txn = datastore.beginTransaction();
		// try {
		// Key employeeKey = KeyFactory.createKey("Employee", "Joe");
		// Entity employee = datastore.get(employeeKey);
		// employee.setProperty("vacationDays", 10);
		//
		// datastore.delete(employee);
		// datastore.get
		// txn.commit();
		// } finally {
		// if (txn.isActive()) {
		// txn.rollback();
		// }
		// }
	}

}
